/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.digitalization.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.checkerframework.checker.units.qual.A;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.digitalization.entity.AuthB01Entity;
import org.springblade.modules.digitalization.mapper.AuthB01Mapper;
import org.springblade.modules.digitalization.service.IAuthB01Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class AuthB01ServiceImpl extends BaseServiceImpl<AuthB01Mapper, AuthB01Entity> implements IAuthB01Service {


	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Transactional
	@Override
	public Integer deleteAbsolute(List<AuthB01Entity> entities) {

		AtomicInteger count = new AtomicInteger(0);
		entities.parallelStream().forEach(authB01Entity -> {
			LambdaQueryWrapper<AuthB01Entity> wrapper = Wrappers.<AuthB01Entity>lambdaQuery()
				.eq(AuthB01Entity::getB01Id, authB01Entity.getB01Id());
			if (authB01Entity.getUserId() == null) {
				wrapper.eq(AuthB01Entity::getDeptId, authB01Entity.getDeptId());
			} else {
				wrapper.eq(AuthB01Entity::getUserId, authB01Entity.getUserId());
			}
			List<AuthB01Entity> list = this.list(wrapper);
			if (list.isEmpty() == false) {
				this.removeBatchByIds(list);
				count.addAndGet(list.size());
			}
		});
		deleteDeletedData();
		return count.get();
	}

	@Override
	public Integer deleteAbsolute(AuthB01Entity entitiy) {
		LambdaQueryWrapper<AuthB01Entity> wrapper = Wrappers.lambdaQuery();
		if (entitiy.getUserId() == null) {
			wrapper.eq(AuthB01Entity::getDeptId, entitiy.getDeptId());
		} else {
			wrapper.eq(AuthB01Entity::getUserId, entitiy.getUserId());
		}
		this.remove(wrapper);
		return 1;
	}

	@Override
	public List<AuthB01Entity> findByUserId(Long userId) {
		LambdaQueryWrapper<AuthB01Entity> wrapper = Wrappers.<AuthB01Entity>lambdaQuery()
			.eq(AuthB01Entity::getUserId, userId);
		return this.list(wrapper);
	}

	@Override
	public List<AuthB01Entity> findByDeptId(Long deptId) {
		return findByDeptIds(deptId.toString());
	}

	@Override
	public List<AuthB01Entity> findByDeptIds(String deptIds) {
		List<Long> ids = Func.toLongList(deptIds);
		LambdaQueryWrapper<AuthB01Entity> wrapper = Wrappers.<AuthB01Entity>lambdaQuery()
			.in(AuthB01Entity::getDeptId, ids);
		return this.list(wrapper);
	}

	private void deleteDeletedData() {
		jdbcTemplate.update("delete from manager_auth_b01 where is_deleted = 1");
	}
}
